package com.financing.platform.util;

import java.io.*;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil {
	
	private static Logger log = Logger.getLogger(FileUtil.class);
	/**
	 * 
	* @Title: saveFile 
	* @Description: 保存文件
	* @param   file
	* @return boolean   
	* @throws
	 */
	public static void saveFile(MultipartFile file,String fileFullPath) throws Exception{
		
			File fileTmp = new File(fileFullPath);

			if(!fileTmp.exists()){
				fileTmp.createNewFile();
				log.info("create new file !");
			}
			file.transferTo(fileTmp);
	}
	/**
	 *
	 * @Title: saveFile
	 * @Description: 保存文件
	 * @return boolean
	 * @throws
	 */
	public static void saveFileInfo(String info,String fileFullPath) throws Exception{

		File fileTmp = new File(fileFullPath);

		if(!fileTmp.exists()){
			fileTmp.createNewFile();
			log.info("create new file !");
			FileOutputStream outputStream = new FileOutputStream(fileTmp);
			outputStream.write(info.getBytes());
		}else {
			FileOutputStream outputStream = new FileOutputStream(fileTmp, true);//第二个参数为追加文本
			outputStream.write((","+info).getBytes());
		}
	}
	/**
	 *
	 * @Title: saveFile
	 * @Description:  读取文件
	 * @return boolean
	 * @throws
	 */
	public static String getFileInfo(String fileFullPath) throws Exception{

		File file = new File(fileFullPath);
		return FileUtils.readFileToString(file);
	}
}
